<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>config &mdash; GOZERWAVE v0.1 BETA documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '0.1 BETA',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="top" title="GOZERWAVE v0.1 BETA documentation" href="../index.html" />
    <link rel="next" title="eventbase" href="eventbase.html" />
    <link rel="prev" title="commands" href="commands.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="eventbase.html" title="eventbase"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="commands.html" title="commands"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">GOZERWAVE v0.1 BETA documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="config">
<h1>config<a class="headerlink" href="#config" title="Permalink to this headline">¶</a></h1>
<div class="section" id="module-gozerlib.config">
<h2>description<a class="headerlink" href="#module-gozerlib.config" title="Permalink to this headline">¶</a></h2>
<p>gozerwave config module.</p>
<dl class="class">
<dt id="gozerlib.config.Config">
<em class="property">class </em><tt class="descclassname">gozerlib.config.</tt><tt class="descname">Config</tt><big>(</big><em>filename=None</em>, <em>verbose=True</em>, <em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#gozerlib.config.Config" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a title="gozerlib.utils.lazydict.LazyDict" class="reference external" href="utils/lazydict.html#gozerlib.utils.lazydict.LazyDict"><tt class="xref docutils literal"><span class="pre">gozerlib.utils.lazydict.LazyDict</span></tt></a></p>
<p>config class is a dict containing json strings. is writable to file 
and human editable.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><em>filename</em> (string) &#8211; filename of the config file</li>
<li><em>verbose</em> (boolean) &#8211; whether loading of config should ne verbose</li>
</ul>
</td>
</tr>
<tr class="field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">self</p>
</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="gozerlib.config.Config.fromdb">
<tt class="descname">fromdb</tt><big>(</big><big>)</big><a class="headerlink" href="#gozerlib.config.Config.fromdb" title="Permalink to this definition">¶</a></dt>
<dd>read config from database.</dd></dl>

<dl class="method">
<dt id="gozerlib.config.Config.fromfile">
<tt class="descname">fromfile</tt><big>(</big><em>filename</em><big>)</big><a class="headerlink" href="#gozerlib.config.Config.fromfile" title="Permalink to this definition">¶</a></dt>
<dd><p>read config object from filename.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameter:</th><td class="field-body"><em>filename</em> (string) &#8211; name of file to write to</td>
</tr>
<tr class="field"><th class="field-name">Return type:</th><td class="field-body">self</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="gozerlib.config.Config.load">
<tt class="descname">load</tt><big>(</big><em>verbose=True</em><big>)</big><a class="headerlink" href="#gozerlib.config.Config.load" title="Permalink to this definition">¶</a></dt>
<dd><p>load the config file.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameter:</th><td class="field-body"><em>verbose</em> &#8211; whether loading should be verbose</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="gozerlib.config.Config.reload">
<tt class="descname">reload</tt><big>(</big><big>)</big><a class="headerlink" href="#gozerlib.config.Config.reload" title="Permalink to this definition">¶</a></dt>
<dd>reload the config file.</dd></dl>

<dl class="method">
<dt id="gozerlib.config.Config.save">
<tt class="descname">save</tt><big>(</big><big>)</big><a class="headerlink" href="#gozerlib.config.Config.save" title="Permalink to this definition">¶</a></dt>
<dd><p>save the config file.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Return type:</th><td class="field-body">integer .. number of lines saved</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="gozerlib.config.Config.set">
<tt class="descname">set</tt><big>(</big><em>item</em>, <em>value</em><big>)</big><a class="headerlink" href="#gozerlib.config.Config.set" title="Permalink to this definition">¶</a></dt>
<dd><p>set item to value.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><em>item</em> (string) &#8211; item to set value of</li>
<li><em>value</em> (dict, list, string, number or boolean) &#8211; value to set</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="gozerlib.config.Config.todb">
<tt class="descname">todb</tt><big>(</big><big>)</big><a class="headerlink" href="#gozerlib.config.Config.todb" title="Permalink to this definition">¶</a></dt>
<dd>save config to database.</dd></dl>

<dl class="method">
<dt id="gozerlib.config.Config.tofile">
<tt class="descname">tofile</tt><big>(</big><em>filename</em><big>)</big><a class="headerlink" href="#gozerlib.config.Config.tofile" title="Permalink to this definition">¶</a></dt>
<dd>save config object to file.</dd></dl>

</dd></dl>

<dl class="function">
<dt id="gozerlib.config.ownercheck">
<tt class="descclassname">gozerlib.config.</tt><tt class="descname">ownercheck</tt><big>(</big><em>userhost</em><big>)</big><a class="headerlink" href="#gozerlib.config.ownercheck" title="Permalink to this definition">¶</a></dt>
<dd><p>check whether userhost is a owner.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameter:</th><td class="field-body"><em>userhost</em> (string) &#8211; userhost to check</td>
</tr>
<tr class="field"><th class="field-name">Return type:</th><td class="field-body">boolean</td>
</tr>
</tbody>
</table>
</dd></dl>

</div>
<div class="section" id="code">
<h2>code<a class="headerlink" href="#code" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274</pre></td><td class="code"><div class="highlight"><pre><span class="c"># gozerlib/config.py</span>
<span class="c">#</span>
<span class="c">#</span>

<span class="sd">&quot;&quot;&quot; </span>
<span class="sd">    gozerwave config module. </span>

<span class="sd">&quot;&quot;&quot;</span>

<span class="n">owner</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;bthate@gmail.com&#39;</span><span class="p">,</span> <span class="s">&#39;bthate@appspot.com&#39;</span><span class="p">,</span> <span class="s">&#39;notauth@127.0.0.1&#39;</span><span class="p">,</span> <span class="s">&#39;test@example.com&#39;</span><span class="p">]</span>

<span class="c"># gozerlib imports</span>

<span class="kn">from</span> <span class="nn">gozerlib.utils.lazydict</span> <span class="kn">import</span> <span class="n">LazyDict</span>
<span class="kn">from</span> <span class="nn">gozerlib.utils.exception</span> <span class="kn">import</span> <span class="n">handle_exception</span>

<span class="c"># simplejson imports</span>

<span class="kn">from</span> <span class="nn">simplejson</span> <span class="kn">import</span> <span class="n">loads</span><span class="p">,</span> <span class="n">dumps</span>

<span class="c"># basic imports</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">types</span>
<span class="kn">import</span> <span class="nn">thread</span>
<span class="kn">import</span> <span class="nn">logging</span>

<span class="c"># END IMPORT</span>

<span class="k">class</span> <span class="nc">Config</span><span class="p">(</span><span class="n">LazyDict</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; </span>
<span class="sd">        config class is a dict containing json strings. is writable to file </span>
<span class="sd">        and human editable.</span>

<span class="sd">        :param filename: filename of the config file</span>
<span class="sd">        :type filename: string</span>
<span class="sd">        :param verbose: whether loading of config should ne verbose</span>
<span class="sd">        :type verbose: boolean</span>
<span class="sd">        :rtype: self</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
        <span class="n">LazyDict</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="n">__file__</span><span class="p">)))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span> <span class="ow">or</span> <span class="s">&#39;config&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cfile</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dir</span> <span class="o">+</span> <span class="n">os</span><span class="o">.</span><span class="n">sep</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">filename</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cfile</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">isdb</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
            <span class="kn">from</span> <span class="nn">gozerlib.persist.persist</span> <span class="kn">import</span> <span class="n">Persist</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cfile</span> <span class="o">=</span> <span class="s">&#39;gozerstore&#39;</span> <span class="o">+</span> <span class="n">os</span><span class="o">.</span><span class="n">sep</span> <span class="o">+</span> <span class="n">filename</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">jsondb</span> <span class="o">=</span> <span class="n">Persist</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cfile</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">isdb</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">configlist</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">verbose</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="n">item</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">dict</span><span class="o">.</span><span class="n">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            set item to value.</span>

<span class="sd">            :param item: item to set value of</span>
<span class="sd">            :type item: string</span>
<span class="sd">            :param value: value to set</span>
<span class="sd">            :type value: dict, list, string, number or boolean</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="nb">dict</span><span class="o">.</span><span class="n">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">fromdb</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot; read config from database. &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">gozerlib.persist.persist</span> <span class="kn">import</span> <span class="n">Persist</span>

        <span class="n">tmp</span> <span class="o">=</span> <span class="n">Persist</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cfile</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">tmp</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">todb</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot; save config to database. &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">jsondb</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">jsondb</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">fromfile</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot; </span>
<span class="sd">            read config object from filename. </span>

<span class="sd">            :param filename: name of file to write to</span>
<span class="sd">            :type filename: string</span>
<span class="sd">            :rtype: self</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">curline</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>

        <span class="c"># read file and set config values to loaded JSON entries</span>
        <span class="n">fname</span> <span class="o">=</span> <span class="n">filename</span>
 
        <span class="c"># open file</span>

        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">fname</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">)</span>

        <span class="c"># loop over data in config file            </span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
            <span class="n">curline</span> <span class="o">=</span> <span class="n">line</span>
            <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="n">line</span> <span class="ow">or</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;#&#39;</span><span class="p">):</span>
                <span class="k">continue</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;=&#39;</span><span class="p">,</span> <span class="mf">1</span><span class="p">)</span>
                <span class="bp">self</span><span class="p">[</span><span class="n">key</span><span class="o">.</span><span class="n">strip</span><span class="p">()]</span> <span class="o">=</span> <span class="n">loads</span><span class="p">(</span><span class="nb">unicode</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">strip</span><span class="p">()))</span>

        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">tofile</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot; save config object to file. &quot;&quot;&quot;</span>

        <span class="n">written</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">curitem</span> <span class="o">=</span> <span class="bp">None</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="c"># read existing config file if available</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">configlist</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
            <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">configlist</span> <span class="o">=</span> <span class="p">[]</span>

            <span class="c"># make temp file</span>
            <span class="n">configtmp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span> <span class="o">+</span> <span class="s">&#39;.tmp&#39;</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span>
            <span class="n">teller</span> <span class="o">=</span> <span class="mf">0</span>

            <span class="c"># write header if not already there</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">configlist</span><span class="p">:</span>
                <span class="n">configtmp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;# </span><span class="si">%s</span><span class="se">\n\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">cfile</span><span class="p">)</span>

            <span class="c"># loop over original lines replacing updated data</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">configlist</span><span class="p">:</span>
                <span class="n">teller</span> <span class="o">+=</span> <span class="mf">1</span>

                <span class="c"># skip comment</span>
                <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;#&#39;</span><span class="p">):</span>
                    <span class="n">configtmp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
                    <span class="k">continue</span>

                <span class="c"># take part after the =</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">keyword</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;=&#39;</span><span class="p">)[</span><span class="mf">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
                    <span class="n">curitem</span> <span class="o">=</span> <span class="n">keyword</span>
                <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>   
                    <span class="n">configtmp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
                    <span class="k">continue</span>

                <span class="c"># write JSON string of data</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="n">keyword</span><span class="p">):</span>  
                    <span class="n">configtmp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> = </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">keyword</span><span class="p">,</span> <span class="n">dumps</span><span class="p">(</span><span class="bp">self</span><span class="p">[</span><span class="n">keyword</span><span class="p">])))</span>
                    <span class="n">written</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">keyword</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">configtmp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>

            <span class="c"># write data not found in original config file</span>
            <span class="k">for</span> <span class="n">keyword</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">written</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="n">curitem</span> <span class="o">=</span> <span class="n">keyword</span>
                <span class="n">configtmp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> = </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">keyword</span><span class="p">,</span> <span class="n">dumps</span><span class="p">(</span><span class="n">value</span><span class="p">)))</span>

            <span class="c"># move temp file to original</span>
            <span class="n">configtmp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="n">os</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">filename</span> <span class="o">+</span> <span class="s">&#39;.tmp&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cfile</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">WindowsError</span><span class="p">:</span>
                <span class="c"># no atomic operation supported on windows! error is thrown when destination exists</span>
                <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
                <span class="n">os</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">filename</span> <span class="o">+</span> <span class="s">&#39;.tmp&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cfile</span><span class="p">)</span>

            <span class="k">return</span> <span class="n">teller</span>

        <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">ex</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;ERROR WRITING </span><span class="si">%s</span><span class="s"> CONFIG FILE: </span><span class="si">%s</span><span class="s"> .. </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cfile</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">ex</span><span class="p">),</span> <span class="n">curitem</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span>
                
    <span class="k">def</span> <span class="nf">load</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            load the config file.</span>

<span class="sd">            :param verbose: whether loading should be verbose</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isdb</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fromdb</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&#39;PRE LOAD config </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">filename</span> <span class="o">==</span> <span class="s">&#39;mainconfig&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;owner&#39;</span><span class="p">,</span> <span class="p">[])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;loglist&#39;</span><span class="p">,</span>  <span class="p">[])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;quitmsg&#39;</span><span class="p">,</span> <span class="s">&quot;http://gozerbot.org&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;debug&#39;</span><span class="p">,</span> <span class="mf">0</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;plugdeny&#39;</span><span class="p">,</span> <span class="p">[])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;dotchars&#39;</span><span class="p">,</span>  <span class="s">&quot; .. &quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;floodallow&#39;</span><span class="p">,</span> <span class="mf">1</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;auto_register&#39;</span><span class="p">,</span> <span class="mf">1</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;ondemand&#39;</span><span class="p">,</span> <span class="mf">1</span><span class="p">)</span>
            <span class="kn">import</span> <span class="nn">gozerlib</span>
            <span class="bp">self</span><span class="p">[</span><span class="s">&#39;version&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&quot;GOZERWAVE </span><span class="si">%s</span><span class="s"> &quot;</span> <span class="o">%</span> <span class="n">gozerlib</span><span class="o">.</span><span class="n">__version__</span>
            <span class="bp">self</span><span class="p">[</span><span class="s">&#39;version&#39;</span><span class="p">]</span> <span class="o">+=</span> <span class="s">&quot;BETA&quot;</span>

        <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&#39;config </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>

        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            save the config file.</span>

<span class="sd">            :rtype: integer .. number of lines saved</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">todb</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">reload</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            reload the config file.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">load</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span>

<span class="k">def</span> <span class="nf">ownercheck</span><span class="p">(</span><span class="n">userhost</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        check whether userhost is a owner.</span>
<span class="sd"> </span>
<span class="sd">        :param userhost: userhost to check</span>
<span class="sd">        :type userhost: string</span>
<span class="sd">        :rtype: boolean</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">userhost</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">False</span>

    <span class="n">config</span> <span class="o">=</span> <span class="n">Config</span><span class="p">(</span><span class="s">&#39;mainconfig&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">userhost</span> <span class="ow">in</span> <span class="n">config</span><span class="p">[</span><span class="s">&#39;owner&#39;</span><span class="p">]:</span>
        <span class="k">return</span> <span class="bp">True</span>

    <span class="k">return</span> <span class="bp">False</span>
</pre></div>
</td></tr></table></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">config</a><ul>
<li><a class="reference external" href="#module-gozerlib.config">description</a></li>
<li><a class="reference external" href="#code">code</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="commands.html"
                                  title="previous chapter">commands</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="eventbase.html"
                                  title="next chapter">eventbase</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/gozerlib/config.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="eventbase.html" title="eventbase"
             >next</a> |</li>
        <li class="right" >
          <a href="commands.html" title="commands"
             >previous</a> |</li>
        <li><a href="../index.html">GOZERWAVE v0.1 BETA documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2009, Bart Thate.
      Last updated on Nov 14, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
    </div>
  </body>
</html>